﻿using System;
using System.Collections.Generic;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.Input;
using System.Linq;

namespace Project3
{
    public class Camera
    {
        GraphicsDeviceManager graphics;
        Ship flyer;

        Vector3 eye;
        Vector3 center;
        Vector3 up = new Vector3(0, 1, 0);
        float fov = MathHelper.ToRadians(90);
        float znear = 10;
        float zfar = 10000;

        float followdistance = 150;
        float height = 100;
        float eyeheight = 0;

        Matrix view;
        Matrix projection;

        public Vector3 Center { get { return center; } set { center = value; } }
        public Vector3 Eye { get { return eye; } set { eye = value; } }

        public Matrix View { get { return view; } }
        public Matrix Projection { get { return projection; } }

        public Camera(GraphicsDeviceManager graphics, Ship flyer)
        {
            this.graphics = graphics;
            this.flyer = flyer;
        }

        public void Initialize()
        {
            ComputeView();
            ComputeProjection();
        }

        private void ComputeView()
        {
            view = Matrix.CreateLookAt(center, eye, up);
        }

        private void ComputeProjection()
        {
            projection = Matrix.CreatePerspectiveFieldOfView(fov,
                graphics.GraphicsDevice.Viewport.AspectRatio, znear, zfar);
        }

        public void Update()
        {
            center = flyer.Position - flyer.Orientation * followdistance;
            center.Y = height;
            eye = flyer.Position;
            eye.Y = (eye.Y - eyeheight) / 2 + eyeheight;
            ComputeView();
        }
    }
}